package com.example.webservice.client;

import com.example.webservice.client.service.HelloService;
import com.example.webservice.client.service.hello.HelloServiceImplService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * @Author hweiyu
 * @Description
 * @Date 2021/4/21 18:04
 */
public class MyClient {

    public static void main(String[] args) throws Exception {
        //方式1，通过代理工厂，根据接口创建代理实现请求
        test1();

        //方式2，通过动态客户端方式请求
        test2();

        //方式3，通过"Generate Java Code From Wsdl"生成代码(idea中选中文件夹右键 -> webservices -> Generate Java Code From Wsdl)
        test3();

    }

    private static void test1() {
        //创建代理工厂
        JaxWsProxyFactoryBean proxy = new JaxWsProxyFactoryBean();
        //设置代理地址
        proxy.setAddress("http://localhost:8080/services/v1/demo?wsdl");
        //设置被代理类接口
        proxy.setServiceClass(HelloService.class);
        //接口代理
        HelloService service = (HelloService) proxy.create();
        //调用
        String response = service.hello("World");
        System.out.println("test1返回结果:" + response);
    }

    private static void test2() throws Exception {
        //创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8080/services/v1/demo?wsdl");
        //调远程方法
        Object[] response = client.invoke("hello", "World");
        System.out.println("test2返回结果:" + response[0]);
    }

    private static void test3() {
        HelloServiceImplService service = new HelloServiceImplService();
        String response = service.getHelloServicePort().hello("World");
        System.out.println("test3返回结果:" + response);
    }
}
